import { message } from "antd";
import * as authMenuService from '../services/menu';

export default {
  namespace: 'authMenu',

  state: {
    list: [],
    data: {}
  },

  effects: {
    *list(_, { call, put }) {
      console.log("page authMenus:");
      const res = yield call(authMenuService.list);
      console.log(res)
      if (res.code === 200) {
        yield put({
          type: 'saveList',
          payload: res.data,
        });
      }
    },
    *get({ payload }, { call, put }) {
      console.log('get authMenu:');
      console.log(payload);
      const res = yield call(authMenuService.get, payload);
      if (res.code === 200) {
        yield put({
          type: 'saveData',
          payload: res.data,
        });
      }
    },
    *add({ payload }, { call, put }) {
      console.log("add authMenu:");
      console.log(payload);
      const res = yield call(authMenuService.add, payload);
      if(res.code === 201){
        message.success('添加成功');
        yield put({
          type: 'list'
        });
      }
    },
    *update({ payload }, { call, put }) {
      console.log("update authMenu:");
      console.log(payload);
      const res = yield call(authMenuService.update, payload);
      if(res.code === 201){
        message.success('更新成功');
        yield put({
          type: 'list'
        });
      }
    },
    *del({ payload }, { call, put }) {
      console.log("delete authMenu:");
      console.log(payload);
      const res = yield call(authMenuService.del, payload);
      if (res.code === 204) {
        message.success('删除成功');
        yield put({
          type: 'list'
        });
        yield put({
          type: 'reset'
        })
      }
    },
    *delBatch({ payload }, { call, put }) {
      console.log("deleteBatch authMenu:");
      console.log(payload);
      const res = yield call(authMenuService.delBatch, payload);
      if (res.code === 204) {
        message.success('删除成功');
        yield put({
          type: 'list'
        });
      }
    },
    *reset(_, {put}){
      yield put({
        type: 'resetData'
      })
    }
  },

  reducers: {
    saveList(state, action) {
      return {
        ...state,
        list: action.payload,
      };
    },
    saveData(state, action) {
      return {
        ...state,
        data: action.payload,
      };
    },
    resetData(state){
      return {
        ...state,
        data: {}
      }
    }
  },
};
